//
//  NSDate+Extension.m
//  黑马微博2期
//
//  Created by apple on 14-10-18.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "NSDate+Extension.h"

@implementation NSDate (Extension)

/**
 *  判断某个时间是否为今年
 */
- (BOOL)isThisYear
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // 获得某个时间的年月日时分秒
    NSDateComponents *dateCmps = [calendar components:NSCalendarUnitYear fromDate:self];
    NSDateComponents *nowCmps = [calendar components:NSCalendarUnitYear fromDate:[NSDate date]];
    return dateCmps.year == nowCmps.year;
}

/**
 *  判断某个时间是否为昨天
 */
- (BOOL)isYesterday
{
    NSDate *now = [NSDate date];
    
    // date ==  2014-04-30 10:05:28 --> 2014-04-30 00:00:00
    // now == 2014-05-01 09:22:10 --> 2014-05-01 00:00:00
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
    
    // 2014-04-30
    NSString *dateStr = [fmt stringFromDate:self];
    // 2014-10-18
    NSString *nowStr = [fmt stringFromDate:now];
    
    // 2014-10-30 00:00:00
    NSDate *date = [fmt dateFromString:dateStr];
    // 2014-10-18 00:00:00
    now = [fmt dateFromString:nowStr];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *cmps = [calendar components:unit fromDate:date toDate:now options:0];
    
    return cmps.year == 0 && cmps.month == 0 && cmps.day == 1;
}

/**
 *  判断某个时间是否为今天
 */
- (BOOL)isToday
{
    NSDate *now = [NSDate date];
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
    
    NSString *dateStr = [fmt stringFromDate:self];
    NSString *nowStr = [fmt stringFromDate:now];
    
    return [dateStr isEqualToString:nowStr];
}

+ (NSDate *)nowDate
{
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:datenow];
    NSDate *localeDate = [datenow  dateByAddingTimeInterval:interval];
    return localeDate;
}

+ (NSString *)timeInterval {
    return [NSString stringWithFormat:@"%ld", (long)[[self nowDate] timeIntervalSince1970]];
}
+ (NSString *)timestamp {
    return [NSString stringWithFormat:@"%ld", (long)[[self nowDate] timeIntervalSince1970]*1000];
}
+ (NSString *)homeTime {
    NSInteger year,month,day,hour,min,sec,week;
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    comps = [calendar components:unitFlags fromDate:[self nowDate]];
    year = [comps year];
    week = [comps weekday];
    month = [comps month];
    day = [comps day];
    hour = [comps hour];
    min = [comps minute];
    sec = [comps second];
    
    NSDateComponents *componets = [[NSCalendar autoupdatingCurrentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
    NSInteger weekday = [componets weekday];//a就是星期几，1代表星期日，2代表星期一，3代表星期二，4代表星期三，5代表星期四，6代表星期五，7代表星期星期六，
    
    NSString *weekStr = nil;
    if (weekday == 1)
    {
        weekStr = @"星期天";
    }
    else if (weekday == 2)
    {
        weekStr = @"星期一";
    }
    else if (weekday == 3)
    {
        weekStr = @"星期二";
    }
    else if (weekday == 4)
    {
        weekStr = @"星期三";
    }
    else if (weekday == 5)
    {
        weekStr = @"星期四";
    }
    else if (weekday == 6)
    {
        weekStr = @"星期五";
    }
    else if (weekday == 7)
    {
        weekStr = @"星期六";
    }
    else
    {
        weekStr = @"";
    }
    
    NSString *string = [NSString stringWithFormat:@"%@",[self nowDate]];
    NSArray *array = [string componentsSeparatedByString:@" "];
    NSString *timeString = [array objectAtIndex:0];
    NSArray *timeArray = [timeString componentsSeparatedByString:@"-"];
    NSInteger monthIndex = [[timeArray objectAtIndex:1] integerValue];
    NSInteger dayIndex = [[timeArray objectAtIndex:2] integerValue];
    
    return [NSString stringWithFormat:@"%ld月%ld号%@%@",(long)monthIndex,(long)dayIndex,@" ",weekStr];
}

+ (NSString *)getYearMonthDay {
    NSInteger year,month,day,hour,min,sec,week;
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *now = [NSDate date];;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    comps = [calendar components:unitFlags fromDate:now];
    year = [comps year];
    week = [comps weekday];
    month = [comps month];
    day = [comps day];
    hour = [comps hour];
    min = [comps minute];
    sec = [comps second];
    
    return [NSString stringWithFormat:@"%ld/%ld/%ld",(long)year,(long)month,(long)day];
}

+ (NSString *)timeString:(int )timeIntervalSince
{
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeIntervalSince];
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:dd:mm"];
    return [formatter stringFromDate:confromTimesp];
}
// 转时间戳
+(NSDate *)timeSimpleWithStr:(NSString *)string {
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSDate* date = [formatter dateFromString:string];
    LKLog(@"date=%@",date);
//    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    return date;
}
@end
